﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Silverlight.Reporting.TestClient.Services;

namespace Silverlight.Reporting.TestClient
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();

            InitializeReport();
        }


        private ReportTotals _totals;

        private void InitializeReport()
        {
            Report.EndPrint += (s, e) =>
                {
                    MessageBox.Show("Report printed.");
                };

            Report.BeginBuildReport += (s, e) =>
                {
                    // initialize running totals
                    _totals = new ReportTotals();
                };

            Report.BeginBuildReportItem += (s, e) =>
                {
                    // this event fires with each report item, so you
                    // can use it to build a running total
                    var item = e.DataContext as EmployeeReviewReportItem;

                    _totals.EmployeeCount++;
                    _totals.TotalBonus += item.Bonus;
                    _totals.TotalSalary += item.Salary;
                };

            Report.BeginBuildReportFooter += (s, e) =>
                {
                    // set the running total as the context for the report footer
                    e.DataContext = _totals;
                };

        }

        private void PrintReport_Click(object sender, RoutedEventArgs e)
        {
            Report.Print();
        }

        private void LoadData_Click(object sender, RoutedEventArgs e)
        {
            var client = new DataServiceClient();

            client.GetEmployeeReviewReportDataCompleted += (s, ea) =>
                {
                    Report.ItemsSource = ea.Result;
                    PrintReport.IsEnabled = ea.Result.Count > 0;
                };

            client.GetEmployeeReviewReportDataAsync();
        }
    }
}
